<template>
  <div class="app-container">
    <!-- 添加或修改扩展插件对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" v-if="open" append-to-body>
      <el-form ref="HostPlugRef" :model="form" :rules="rules" label-width="110px" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="作者" prop="author">
              <el-input v-model="form.author" placeholder="请输入作者" maxlength="100" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="插件名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入插件名称" maxlength="100" show-word-limit/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="官方文档" prop="website">
              <el-input v-model="form.website" placeholder="请输入官方文档" maxlength="255" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="选择插件jar文件" prop="jarHttpUrl">
              <file-upload v-model="form.jarHttpUrl"  :fileType="['jpg','png','jar']" :fileSize="300"></file-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" v-if="!disabled" @click="submitForm">确 定</el-button>
          <el-button @click="cancel" v-if="!disabled">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dialog">
import { getHotPlug, addHotPlug, updateHotPlug } from "@/api/system/hotPlug";

const { proxy } = getCurrentInstance();

const open = ref(false);
const disabled = ref(false);
const title = ref("");

const data = reactive({
  form: {},
  rules: {
  }
});

const emit = defineEmits(["refresh"]);
const { form, rules } = toRefs(data);

/** 打开扩展插件弹框 */
async function show(qualifiedName, _disabled) {
  reset();
  disabled.value = _disabled
  if (qualifiedName) {
    await getHotPlug(qualifiedName).then(response => {
      form.value = response.data;
    });
  }
  open.value = true
  if (disabled.value) {
    title.value = "详情"
  } else if (qualifiedName) {
    title.value = "修改"
  } else {
    title.value = "新增"
  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    qualifiedName: null,
    author: null,
    website: null,
    jarName: null,
    tenantId: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("HostPlugRef");
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["HostPlugRef"].validate(valid => {
    if (valid) {
      if (form.value.qualifiedName != null) {
        updateHotPlug(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          emit('refresh');
        });
      } else {
        addHotPlug(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          emit('refresh');
        });
      }
    }
  });
}


defineExpose({
  show,
});
</script>
